package cn.dengta.webapp.trade.entity.meta;

import java.math.BigDecimal;
import java.util.Map;

import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.trade.entity.meta.MetaConfig.MetaRuleConfig;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.*;

/**
 * 速配奖励的规则配置对象
 */
@Getter
@Setter
public class SpeedDatingRewardRule implements MetaRuleConfig {

	private Map<WesKit, SpeedDatingRewardEntry[]> config;

	public SpeedDatingRewardEntry[] findByWesKit(WesKit wesKit) {
		return X.isValid(config) ? config.get(wesKit) : null;
	}

	@Override
	public void validate() {
		Assert.isTrue(X.isValid(config), "速配奖励配置不能为空！");
		for (Map.Entry<WesKit, SpeedDatingRewardEntry[]> reward : this.config.entrySet()) {
			SpeedDatingRewardEntry[] entries = reward.getValue();
			Assert.isTrue(X.size(entries) > 0, "速配奖励配置不能为空！");
			for (int i = entries.length - 1; i > 0; ) {
				SpeedDatingRewardEntry entry = entries[i--];
				entry.validate();
				SpeedDatingRewardEntry nextEntry = entries[i];
				nextEntry.validate();
				Assert.isTrue(entry.duration >= nextEntry.duration, "速配时长只能递增！");
				Assert.isTrue(entry.integralAward.compareTo(nextEntry.integralAward) >= 0, "补贴奖励只能递增！");
			}
		}
	}

	@Override
	public void init(MetaRuleConfig config) {
		SpeedDatingRewardRule rule = (SpeedDatingRewardRule) config;
		this.config = rule.config;
	}

	@JSONField(serialize = false)
	public SpeedDatingRewardEntry getEntry(WesKit wesKit, SpeedDatingRewardEntry input, boolean defaultValue) {
		final SpeedDatingRewardEntry[] entries = findByWesKit(wesKit);
		SpeedDatingRewardEntry entry;
		for (int i = entries.length - 1; i >= 0; i--) {
			entry = entries[i];
			if (input.duration >= (entry.duration * 60)) {
				return entry;
			}
		}
		return defaultValue ? entries[0] : null;
	}

	@JSONField(serialize = false)
	public static SpeedDatingRewardEntry getEntry(SpeedDatingRewardEntry[] entries, Long duration, boolean defaultValue) {
		if (X.isValid(entries) && Cmp.nullToZero(duration) > 0) {
			SpeedDatingRewardEntry entry;
			for (int i = entries.length - 1; i >= 0; i--) {
				entry = entries[i];
				if (duration >= (entry.duration * 60)) {
					return entry;
				}
			}
		}
		return defaultValue ? entries[0] : null;
	}

	/**
	 * 速配奖励规则实体
	 */
	@Setter
	@Getter
	public static class SpeedDatingRewardEntry {

		/** 名称 */
		protected String name;
		/** 速配时长（分） */
		protected Long duration;
		/** 满足以上条件获得奖励（积分） */
		protected BigDecimal integralAward;

		public SpeedDatingRewardEntry() {
		}

		public SpeedDatingRewardEntry(String name, Long duration, BigDecimal integralAward) {
			this.name = name;
			this.duration = duration;
			this.integralAward = integralAward;
		}

		public SpeedDatingRewardEntry init(String name, Long duration, BigDecimal integralAward) {
			this.name = name;
			this.duration = X.expectNotNull(duration, 0L);
			this.integralAward = X.expectNotNull(integralAward, BigDecimal.ZERO);
			return this;
		}

		public void validate() {
			Assert.isTrue(StringUtil.notEmpty(name), "名称不能为空！");
			Assert.isTrue(duration > 0, "速配时长不能小于0！");
			Assert.isTrue(integralAward.compareTo(BigDecimal.ZERO) > 0, "奖励必须大于0！");
		}

	}

}
